﻿using System;
using GeoAPI.Geometries;
using YJKGridTopologyCal.Geometries;
using YJKGridTopologyCal.Geometries.Implementation;
using YJKGridTopologyCal.IO;

namespace YJKGridTopologyCal.Noding
{
	public class BasicSegmentString : ISegmentString
	{
		public BasicSegmentString(Coordinate[] pts, object data)
		{
			this._pts = pts;
			this.Context = data;
		}

		public object Context { get; set; }

		public Coordinate[] Coordinates
		{
			get
			{
				return this._pts;
			}
		}

		public bool IsClosed
		{
			get
			{
				return this._pts[0].Equals2D(this._pts[this._pts.Length]);
			}
		}

		public int Count
		{
			get
			{
				return this._pts.Length;
			}
		}

		public Octants GetSegmentOctant(int index)
		{
			if (index != this._pts.Length - 1)
			{
				return Octant.GetOctant(this._pts[index], this._pts[index + 1]);
			}
			return Octants.Null;
		}

		public LineSegment this[int index]
		{
			get
			{
				if (index < 0 || index >= this.Count)
				{
					throw new ArgumentOutOfRangeException("index", index, "Parameter must be greater than or equal to 0 and less than TotalItemCount.");
				}
				return new LineSegment(this._pts[index], this._pts[index + 1]);
			}
			set
			{
				throw new NotSupportedException("Setting line segments in a ISegmentString not supported.");
			}
		}

		public override string ToString()
		{
			return WKTWriter.ToLineString(new CoordinateArraySequence(this._pts));
		}

		private readonly Coordinate[] _pts;
	}
}
